Shader "ditherScreenPos"
{
    Properties
    {
        _MainTex ("MainTex", 2D) = "white" {}
        _Range("Range", Range(0,0.2)) = 0.02
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct v2f
            {
                float2 uv : TEXCOORD0;
                //float4 vertex : SV_POSITION;
                float4 srcPos : VPOS;//视角空间的坐标
                //float4 srcPos : TEXCOORD1;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            float _Range;

            v2f vert (
                //appdata_full v
                float4 vertex : POSITION,
                float2 texcoord : TEXCOORD0,
                out float4 outPos : SV_POSITION //裁剪空间位置输出 
                )
            {
                v2f o;
                outPos = UnityObjectToClipPos(vertex);
                o.uv = TRANSFORM_TEX(texcoord, _MainTex);
                //o.srcPos = ComputeGrabScreenPos(o.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.uv);

                //自己想的公式 其实差不多，floor其实比round
                float2 checker1 = (round(frac(i.srcPos.xy * _Range))-0.5) * 2 ;
                //float checker = checker1.x * checker1.y  ;


                //may的公式
                float2 screenPos = floor(i.srcPos.xy * _Range) * 0.5;
                float checker = -frac(screenPos.x + screenPos.y)  ;

                clip(checker);
                
                return col;
            }
            ENDCG
        }
    }
}
